module game
{
	export class EditorMainUI extends eui.Component
	{
		private mapBtn: eui.Button;
		private gridBtn: eui.Button;
		private crossBtn: eui.Button;
		private saveBtn: eui.Button;
		private shareBtn: eui.Button;
		/**预览的界面 */
		private preview: eui.Group;

		private mapEditor: eui.Panel;
		private crossEditor: eui.Panel;
		private overPanel:eui.Panel;
		private overTxt:eui.Label;
		private qrPanel:eui.Panel;

		private map_json: EditorData;
		private mapGroup: eui.Group;
		private editorMap: EditorMap;
		private myScene: BaseScene;

		private tag1: eui.Label;
		private tag2: eui.Label;
		private tag3: eui.Label;
		private steps: eui.Label;
		private accTxt:eui.Label;
		private ownerTxt:eui.Label;

		private input_tag1: eui.Label;
		private input_tag2: eui.Label;
		private input_tag3: eui.Label;
		private input_steps: eui.Label;

		private bagList:eui.List;
		private listArr:eui.ArrayCollection;
		private bagPanel:eui.Panel;
		private bagBtn:eui.Button;

		private mapId:number;

		public constructor(mapId:number = null)
		{
			super();
			this.mapId = mapId;
			this.skinName = "resource/game_ui/Editor.exml";
			this.addEventListener(eui.UIEvent.CREATION_COMPLETE, this.init, this);
		}

		private addItem():void
		{
			if(!GlobalData.accData){return;}
			let arr = GlobalData.accData.maps;
			if(arr && arr.length > 0)
			{
				for(let i = arr.length - 1;i >= 0;i--)
				{
					this.listArr.addItem({id:arr[i]});
				}

				this.bagPanel.visible = true;
			}
			else
			{
				this.bagPanel.visible = false;
			}
			this.listArr.refresh();
		}


		protected init(): void
		{
			this.removeEventListener(eui.UIEvent.CREATION_COMPLETE, this.init, this);
			this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeListener, this);
			this.mapEditor.visible = false;
			this.shareBtn.visible = false;
			this.mapEditor.closeButton.label = "确定";
			this.crossEditor.visible = false;
			this.listArr = new eui.ArrayCollection();
			this.bagList.useVirtualLayout = false;
			this.bagList.itemRenderer = BagItem;
			this.bagList.dataProvider = this.listArr;
			// this.bagList.validateNow();

			this.addItem();

			if(this.mapId)
			{
				this.map_json = GlobalData.mapData.data;
				this.accTxt.text = GlobalData.mapData.ower;
				switch(this.mapId)
				{
					case 1:
						this.mapBtn.visible = false;
						this.gridBtn.visible = false;
						this.crossBtn.visible = false;
						this.saveBtn.visible = false;
						break;
				}
			}
			else
			{
				this.map_json = <EditorData>RES.getRes("scene1_json");
				this.ownerTxt.visible = false;
				this.accTxt.visible = false;

			}
			
			this.editorMap = new EditorMap();

			this.showMap(0);
			this.initListener();
		}

		private showMap(index: number): void
		{
			let map = this.map_json.map;
			let row = this.map_json.row;
			let col = this.map_json.col;

			if (!map)
			{
				console.error("error none of this map index:", index);
			}
			this.myScene = new BaseScene();
			this.myScene.init(map, row, col, this.map_json.name, this.map_json.default, this.map_json.types);

			this.preview.addChild(this.myScene);

			this.editorMap.init(map, row, col);
			this.mapGroup.addChild(this.editorMap);
		}

		public initListener(): void
		{
			this.mapBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.gridBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.crossBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.saveBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.shareBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.bagBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);

			this.bagList.addEventListener(GameEvent.BagItem_Tap,this.tapItem,this);
			this.bagList.addEventListener(GameEvent.BagItem_Share,this.tapItem,this);

			this.myScene.addEventListener(GameEvent.RemovePop, this.checkTarget, this);
			this.myScene.addEventListener(GameEvent.ReduceSteps, this.reduceSteps, this);

			this.mapEditor.addEventListener(eui.UIEvent.CLOSING, this.onClose, this);
			this.crossEditor.addEventListener(eui.UIEvent.CLOSING, this.onClose, this);
			this.overPanel.addEventListener(eui.UIEvent.CLOSING, this.onClose, this);
			this.bagPanel.addEventListener(eui.UIEvent.CLOSING, this.onClose, this);
			this.qrPanel.addEventListener(eui.UIEvent.CLOSING, this.onClose, this);
		}

		public removeListener(): void
		{
			this.removeEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeListener, this);

			this.mapBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.gridBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.crossBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.saveBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.shareBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);
			this.bagBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onTap, this);

			this.bagList.removeEventListener(GameEvent.BagItem_Tap,this.tapItem,this);
			this.bagList.removeEventListener(GameEvent.BagItem_Share,this.tapItem,this);

			this.myScene.removeEventListener(GameEvent.RemovePop, this.checkTarget, this);
			this.myScene.removeEventListener(GameEvent.ReduceSteps, this.reduceSteps, this);

			this.mapEditor.removeEventListener(eui.UIEvent.CLOSING, this.onClose, this);
			this.crossEditor.removeEventListener(eui.UIEvent.CLOSING, this.onClose, this);
			this.overPanel.removeEventListener(eui.UIEvent.CLOSING, this.onClose, this);
			this.bagPanel.removeEventListener(eui.UIEvent.CLOSING, this.onClose, this);
			this.qrPanel.removeEventListener(eui.UIEvent.CLOSING, this.onClose, this);
		}

		private checkTarget(e: egret.Event): void
		{
			console.warn(e.data);
		}

		private remainSteps = 30;

		private reduceSteps(e: egret.Event): void
		{
			this.remainSteps--;
			if (this.remainSteps < 0)
			{
				this.showOver(false);
				return;
			}
			this.steps.text = this.remainSteps + "";
		}

		private onClose(e: eui.UIEvent): void
		{
			e.preventDefault();
			switch (e.target)
			{
				case this.mapEditor:
					this.map_json.map = this.editorMap.map;
					this.myScene.changeMap(this.editorMap.map);
					break;
				case this.crossEditor:
					this.tag1.text = this.input_tag1.text;
					this.tag2.text = this.input_tag2.text;
					this.tag3.text = this.input_tag3.text;
					this.steps.text = this.input_steps.text;
					this.remainSteps = parseInt(this.steps.text);
					this.map_json.steps = this.remainSteps;
					break;
			}
			e.target.visible = false;
		}

		private tapItem(e:egret.Event):void
		{
			let data = e.data;
			switch(e.type)
			{
				case GameEvent.BagItem_Share:
					let sprite = qr.QRCode.create(window.location.href + "?mapid=" + data.id);
					this.qrPanel.addChild(sprite);
					sprite.x = (this.qrPanel.width - sprite.width)/2;
					sprite.y = (this.qrPanel.height - sprite.height)/2;

					this.qrPanel.visible = true;
					this.addChild(this.qrPanel);
					break;
				case GameEvent.BagItem_Tap:
					GameManager.getInstance().getMapById(data.id + "",this.mapBack,this);
					break;
			}
			
			console.warn(data);
			
		}

		private mapBack():void
		{
			console.warn("map back...");
			this.bagPanel.visible = false;
			this.myScene.changeMap(GlobalData.mapData.data.map);
		}

		private onTap(e: egret.TouchEvent): void
		{
			switch (e.target)
			{
				case this.mapBtn:
					this.mapEditor.visible = true;
					break;
				case this.gridBtn:
					console.warn("暂时没有功能....");
					break;
				case this.crossBtn:
					this.crossEditor.visible = true;
					break;
				case this.bagBtn:
					this.bagPanel.visible = true;
					break;
				case this.saveBtn://想服务器发消息
					// this.crossEditor.visible = true;
					// this.showOver(true);
					GameManager.getInstance().upload_map(this.map_json,this.upComplete,this);
					break;
				case this.shareBtn:
					// this.crossEditor.visible = true;
					UIManager.copyTxt(window.location.href + "?mapid=" + GlobalData.mapId);
					let sprite = qr.QRCode.create(window.location.href + "?mapid=" + GlobalData.mapId);
					this.qrPanel.addChild(sprite);
					sprite.x = (this.qrPanel.width - sprite.width)/2;
					sprite.y = (this.qrPanel.height - sprite.height)/2;

					this.qrPanel.visible = true;
					// this.shareBtn.label = "复制成功";
					break;
			}
		}
		
		private upComplete():void
		{
			this.saveBtn.label = "上传成功";
			this.shareBtn.visible = true;
		}

		private showOver(flag: boolean)
		{
			if(flag)
			{
				this.overTxt.text = "恭喜过关！！";
			}
			else
			{
				this.overTxt.text = "挑战失败，再接再厉！！";
			}

			this.overPanel.visible = true;
		}
	}
}